
import 'serial_port_platform_interface.dart';

class SerialPort {
  /// Constructs a singleton instance of [SerialPort].
  ///
  /// [SerialPort] is designed to work as a singleton.
  // When a second instance is created, the first instance will not be able to listen to the
  // EventChannel because it is overridden. Forcing the class to be a singleton class can prevent
  // misuse of creating a second instance from a programmer.
  factory SerialPort() {
    _singleton ??= SerialPort._();
    return _singleton!;
  }

  SerialPort._();

  static SerialPort? _singleton;

  static SerialPortPlatform get _platform{
    return SerialPortPlatform.instance;
  }

  Future<String?> getPlatformVersion() {
    return _platform.getPlatformVersion();
  }

  Future<bool?> start(String device, baudrate) {
    return _platform.start(device, baudrate);
  }

  Future<void> setLog(bool value) {
    return _platform.setLog(value);
  }

  Future<bool?> close() {
    return _platform.close();
  }

  /// Fires whenever the serial port data changes.
  Stream<String> get onSerialPortDataChanged {
    return _platform.onSerialPortDataChanged;
  }
}
